using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using SlimDX;
using SlimDX.Direct3D9;
using SlimDX.Windows;

using miciv.Rendering;
using miciv.Geometry;

namespace miciv
{
    public class ColorRectangle : Frame, IDiffuse
    {
        protected struct Vertex
        {
            public Vector3 Position;
            public Color4 Color;
        }

        protected Vertex[] m_vertices;
        protected Color4 m_f4Diffuse;
        protected int m_sVertexCount;

        public ColorRectangle(CoreContext _coreContext, string _strID)
            : base(_coreContext, _strID)
        {
        }

        public ColorRectangle(CoreContext _coreContext, string _strID, Frame _frmParent)
            : base(_coreContext, _strID, _frmParent)
        {
        }

        override protected void InternalInitialize(CoreContext _coreContext, string _strID)
        {
            base.InternalInitialize(_coreContext, _strID);
            this.Size = new Vector2(10.0f, 10.0f);
            this.Diffuse = new Color4(1.0f, 1.0f, 1.0f, 1.0f);
            this.m_renderer = this.m_coreContext.GetData<RendererManager>("RendererManager").GetByVertexDefinition("DiffuseVertexDefinition", this);
            this.m_sVertexCount = 4;
        }

        override public Frame Clone(Frame _frmParent, string _strID)
        {
            Frame frame = new ColorRectangle(this.m_coreContext, this.m_strID + _strID, _frmParent);
            frame.Copy(this);
            foreach (Frame child in this.m_lstChildren)
            {
                child.Clone(frame);
            }
            return frame;
        }

        override public Frame Clone(Frame _frmParent)
        {
            return this.Clone(_frmParent, "");
        }

        override public void Copy(Frame _frame)
        {
            base.Copy(_frame);
            ColorRectangle tcr = _frame as ColorRectangle;
            this.Diffuse = tcr.Diffuse;
        }

        override public void UpdateData(float _deltaTime)
        {
            base.UpdateData(_deltaTime);
            if ((false != this.m_bVisible) && (false != this.m_bDirty))
            {
                this.BuildShapeVertexFeeder();
                this.m_bDirty = false;
            }
        }

        protected bool BuildShapeVertexFeeder()
        {
            IShape shape = this.m_coreContext.GetData<ShapeManager>("ShapeManager").Create("rectangle");
            shape.VertexDefinition = this.m_coreContext.GetData<VertexDefinitionManager>("VertexDefinitionManager").Get("DiffuseVertexDefinition");
            shape.Size = new Vector3(this.Size, 0.0f);
            shape.LevelOfDetail = 1;
            bool bResult = shape.Build(this.UserDataOverrideHandler);
            if (false != bResult)
            {
                if (null != this.m_vertexFeeders)
                {
                    this.Renderer.ReleaseFeeders(this.m_vertexFeeders);
                }
                this.m_vertexFeeders = this.Renderer.CreateFeeders(shape);
            }
            return ((false != bResult) && (null != this.m_vertexFeeders) && (0 < this.m_vertexFeeders.Count));
        }

        protected object UserDataOverrideHandler(string _strChannel, int _sIndex)
        {
            if ("COLOR" == _strChannel)
            {
                return this.Diffuse;
            }
            return null;
        }

        #region IColor Membres

        public Color4 Diffuse
        {
            get
            {
                return this.m_f4Diffuse;
            }
            set
            {
                this.m_f4Diffuse = value;
                this.m_bDirty = true;
            }
        }

        #endregion
    }
}
